
package eteam.aps.common.config;

import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.nio.charset.StandardCharsets;

/**
 * 
 *
 * @author galle.yi
 * @date 2021年11月20日 下午8:15:47
 * @see project code007
 */
@Configuration
public class RedisConfig extends CachingConfigurerSupport {

    /**
     * Caused by: java.lang.ClassNotFoundException:
     * org.apache.commons.pool2.impl.GenericObjectPoolConfig
     * 
     * java.util.time Could not read JSON: Cannot construct instance 
     * 
     * 给 field 加 注解
     * #@JsonDeserialize(using = LocalDateTimeDeserializer.class) 
     * #@JsonSerialize(using = LocalDateTimeSerializer.class)
     * 
     * @author galle.yi
     * @date 2021年11月20日 下午8:17:14
     */
    @Bean
    public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
        template.setConnectionFactory(factory);

        template.setKeySerializer(new StringRedisSerializer(StandardCharsets.UTF_8));
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());

        // hashmap 也要 设置 Serializer；否则 会默认JdkSerializationRedisSerializer
        template.setHashKeySerializer(template.getKeySerializer());
        template.setHashValueSerializer(template.getValueSerializer());
        template.afterPropertiesSet();
        return template;
    }

}
